
#include <stdlib.h>
#include <string.h>
#include "double-stack.h"



int InitStack(DubStack *stack){
    stack->capacity = MAX_STACK_SIZE;
    stack->data = malloc(stack->capacity*sizeof(ElemType));
    stack->top1 = 0;
    stack->top2 = (int)stack->capacity-1;
    return(0);
}

int DestroyStack(DubStack *stack){
    free(stack->data);
    stack->data == NULL;
    stack->top1 = -1;
    stack->top2 = -1;
    stack->capacity = 0;
    return(0);
}

int ClearStack(DubStack *stack,stack_name name){
    if (stack == NULL || stack->data == NULL || stack->capacity == 0){
        return(-1);
    } else{
        memset(stack->data,0, MAX_STACK_SIZE*sizeof(ElemType));
        stack->top1 = 0;
        stack->top2 = (int)stack->capacity-1;
        return(0);
    }
}

int StackEmpty(DubStack stack,stack_name name){
    switch (name) {
        case a:{
            return(stack.top1 == 0);
        }
        case b:{
            return(stack.top2 == stack.capacity-1);
        }
        default:{
            return(-1);
        }
    }
}

int GetTop(DubStack stack,ElemType *e,stack_name name){

}

int Push(DubStack *stack,ElemType e,stack_name name){
    if (stack == NULL || stack->data == NULL || stack->capacity == 0){
        return(-1);
    }
    switch (name) {
        case a:{
            if (stack->top1+1 == stack->top2){
                return(-1);
            }
            memcpy(&stack->data[++stack->top1],&e, sizeof(ElemType));
            return(0);
        }
        case b:{
            if(stack->top2-1 == stack->top1){
                return(-1);
            }
            memcpy(&stack->data[--stack->top2],&e, sizeof(ElemType));
            return(0);
        }
        default:{
            return(-1);
        }
    }
}
int Pop(DubStack *stack,ElemType *e,stack_name name){
    if (stack == NULL || stack->data == NULL || stack->capacity == 0){
        return(-1);
    }
    switch (name) {
        case a:{
            if (stack->top1 == 0){
                return(-1);
            }
            memcpy(e,&stack->data[stack->top1--],sizeof(ElemType));
            return(0);
        }
        case b:{
            if(stack->top2 == stack->capacity-1){
                return(-1);
            }
            memcpy(&e,&stack->data[stack->top2++],sizeof(ElemType));
            return(0);
        }
        default:{
            return(-1);
        }
    }
}
int StackLength(DubStack stack,stack_name name){
    if (stack.data == NULL || stack.capacity == 0){
        return(-1);
    }
    switch (name) {
        case a:{
            return(stack.top1+1);
        }
        case b:{
            return((int)stack.capacity-stack.top2);
        }
        default:{
            return(-1);
        }
    }
}